package cn.finder.httpcommons;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class EnumObject<T> implements Comparable<T>,Cloneable,Serializable {

	
	 /**
	 * 
	 */
	private static final long serialVersionUID = -4262324947998213951L;

	protected List<T> values =new ArrayList<T>();

     protected  T value;

     /// <summary>
     /// 获取本身的值
     /// </summary>
     public  T getValue()
     {
         return this.value;
     }

     /// <summary>
     /// 获取所有的值
     /// </summary>
     public List<T> getValues()
     {
         return this.values;
     }
     public EnumObject(T value)
     {
         this.value = value;
         this.getValues().clear();
         this.getValues().add(value);
     }

     public EnumObject()
     {
     }
     
     public EnumObject(EnumObject<T> item)
     {
         this.value = item.value;
         this.values = item.getValues();
     }


     /// <summary>
     /// 把多个 值加入到集合
     /// </summary>
//     public static T Default = default(T);

     @SuppressWarnings("unchecked")
	public EnumObject<T> or(EnumObject<T> item)
     {

         if (this.getValue() == null)
         {
             try {
				return (EnumObject<T>)item.clone();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         }
         EnumObject<T> newThis=null;
		try {
			newThis = (EnumObject<T>)this.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
         
         newThis.values.addAll(item.getValues());
         return newThis;
     }


     /// <summary>
     /// 移除一条
     /// </summary>
     /// <param name="item"></param>
     /// <returns></returns>
     public EnumObject<T> remove(EnumObject<T> item)
     {

         if (this.getValue() != null)
         {
             if (this.values.contains(item.getValue()))
             {

                 this.getValues().remove(item.getValue());
             }
         }
         return this;
     }


    /***
     * 获取所有的值 逗号隔开
     */
     @Override
     public  String toString()
     {
         StringBuffer sb = new StringBuffer();
         for (T item : values)
         {
             sb.append(",");
             sb.append(item.toString());
         }
         if(sb.toString().length()>0)
             sb.delete(0, 1);
         return sb.toString();
     }


     @SuppressWarnings("unchecked")
	public int compareTo(Object o) {
		EnumObject<T> x_obj = (EnumObject<T>)o;
		return this.value.toString().compareTo(x_obj.value.toString());
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		
		 EnumObject<T> newEnumObj = null;
		 
		 newEnumObj=new EnumObject<T>();
		
		 newEnumObj.value = newEnumObj.getValue();
		
		 for(T t:this.getValues()){
			 newEnumObj.getValues().add(t);
		 }
		 
		 
		 
		 return newEnumObj;
		
	}

     
     
     

}
